package com.fili.gui.components.table.color;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 22, 2006
 * Time: 10:44:46 PM
 */
public class ColorTable extends JComponent implements PropertyChangeListener
{
    private CheckColorBox[] boxes;
    private ColorTableListener listener;

    public ColorTable(int nrItems, int nrRows, ColorTableListener listener)
    {
        this.listener = listener;
        int colNum = (nrItems + nrRows - 1) / nrRows;
        setLayout(new GridLayout(nrRows, colNum));
        boxes = new CheckColorBox[nrItems];
        for (int i = 0; i < nrItems; i++)
        {
            boxes[i] = new CheckColorBox(listener.colorTableSelectedAt(i), listener.colorTableColorAt(i), listener.colorTableTextAt(i));
            add(boxes[i]);
            boxes[i].addPropertyChangeListener(this);
        }
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().equals(CheckColorBox.PROP_COLOR))
        {
            for (int i = 0; i < boxes.length; i++)
                if (boxes[i].equals(evt.getSource()))
                    listener.colorTableColorChanged(i, (Color) evt.getNewValue());
        }
        if (evt.getPropertyName().equals(CheckColorBox.PROP_SELECTION))
        {
            for (int i = 0; i < boxes.length; i++)
                if (boxes[i].equals(evt.getSource()))
                    listener.colorTableSelectionChanged(i, (Boolean) evt.getNewValue());
        }

    }
}
